package service.impl;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import model.Type;
import service.ITypeService;

/**
 * Created by ykj on 16/1/8.
 */
public class TypeServiceImpl implements ITypeService {
    public Page findByPage(int page, int pageSize) {
        return Type.dao.paginate(page, pageSize, "select * from type");
    }

    public Model save(Model model) {
        Type type = (Type) model;
        Long exists = Db.queryLong("select 1 from type where name = ?", type.getName());
        //已存在相同记录
        if(exists != null) return null;
        boolean result = type.save();
        return result ? type : null;
    }

    public Model update(Model model) {
        Type type = (Type) model;
        boolean result = type.update();
        return result ? type : null;
    }

    public boolean delete(int id) {
        return Type.dao.deleteById(id);
    }

    public Model findById(int id) {
        return Type.dao.findById(id);
    }
}
